var ns;
var app = new Vue({
    el: '#app',
    data: {
        funList: [],
        form: {
            UserID: '',
            LoginName: '',
            Realname: '',
            PhoneNumber: '',
            selectlist: [],
        },
    },
    methods: {
        get_data: function() {
            var that = this;
            //判断是新增还是编辑
            if (that.form.UserID != '') {
                //调用接口
                // that.form.selectlist=['2','3'];
                that.get_funList('edit', that.form.selectlist);
            } else {
                that.get_funList();
            }


        },
        get_funList: function(type, selList) {
            var that = this;
            //新增、编辑时调用获取功能权限数据

            //数据处理
            if (type!= '') {
                //渲染选中值
                that.list[0].isselect=false;
                if (selList.length > 0) {
                    for (var i = 0; i < selList.length; i++) {
                        for (var j = 0; j < that.list.length; j++) {
                            if (selList[i] == that.list[j].funId) {
                                that.list[j].isselect = true;
                            }
                        }
                    }
                }
            } else {
                that.form.selectlist.push(that.list[0].funId);
            }

        },

        get_gongneng:function(){
            var that=this;
            var option={};
            option.action="getsysmenuempowerlist";
            option.UserID=0;
            console.log(JSON.stringify(option));
            ns.post(option, function (succeed, data, err) {
                console.log(JSON.stringify(err));
                 console.log(JSON.stringify(data));
                 if(succeed){
                    if(data.status){
                      that.funList=data.list;
                    }else{
                        api.toast({
                            msg: data.msg,
                            duration: 2000,
                            location: 'bottom'
                          });
                    }
           
                  }else{
                   api.toast({
                     msg: err,
                     duration: 2000,
                     location: 'bottom'
                   });
                }
           

             });


        },
        
        do_choose: function(item) {
            var that = this;
            item.checked = !item.checked;
            var removeindex = 0;
            for (var i = 0; i < that.form.selectlist.length; i++) {
                if (that.form.selectlist[i] == item.id) {
                    removeindex = i;
                }
            }
            if (item.checked) {
                that.form.selectlist.push(item.id);
            }
            if (!item.checked) {
                that.form.selectlist.splice(removeindex, 1);
            }

        },
        open_addzh: function() {
            var that = this;
            var option={};
            if(that.form.UserID){
                option.UserID=that.form.UserID;
            }else{
                option.UserID=0;
            }
            option.LoginName=that.form.LoginName;
            option.Realname=that.form.Realname;
            option.PhoneNumber=that.form.PhoneNumber;
            option.SysMenuIDList=JSON.stringify(that.form.selectlist);
            option.action='saveuser';
            ns.post(option, function (succeed, data, err) {
                 if(succeed){
                    if(data.status){
                        api.toast({
                            msg: data.msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                        api.sendEvent({
                            name:'load_zhanghao'
                        });
                        setTimeout(() => {
                            api.closeWin();
                        }, 500);

                    }else{
                        api.toast({
                            msg: data.msg,
                            duration: 2000,
                            location: 'bottom'
                          });
                    }
           
                  }else{
                   api.toast({
                     msg: err,
                     duration: 2000,
                     location: 'bottom'
                   });
                }
             });
        },
        do_save: function() {
            var that = this;
            //新增子账户
            // alert(JSON.stringify(that.form.selectlist))

        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.UserID = api.pageParam.id;
    // app.get_data();
    app.get_gongneng();
};
